package com.ruoyi.video.server.onvif;

import com.ruoyi.video.domain.OnvifCamera;
import com.ruoyi.video.domain.VideoCamera;
import com.ruoyi.video.domain.VideoServer;
import com.ruoyi.video.server.AbstractServer;
import com.ruoyi.video.server.push.PushMessage;
import com.ruoyi.video.server.push.PushServer;
import com.ruoyi.video.server.onvif.lib.BroadcastDiscovery;
import com.ruoyi.video.service.IVideoCameraService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * ONVIF协议发现服务
 */
@Component("onvifServer")
@Slf4j
public class OnvifServer extends AbstractServer {
    public static Integer ONLINE_KEEP = 60 * 1000 * 60;
    public static String SERVER_CODE = "onvif";
    private static Boolean running = false;

    @Autowired
    private IVideoCameraService videoCameraService;
    @Autowired
    private DiscoveryDeviceImpl discoveryDevice;
    @Autowired
    private PushServer pushServer;

    @Override
    public String getCode() {
        return SERVER_CODE;
    }

    @Override
    public boolean start(VideoServer videoServer) {
        this.videoServer = videoServer;

        BroadcastDiscovery.start(discoveryDevice);

        Map<String,String> config = videoConfigService.selectVideoServerConfig(SERVER_CODE);
        String onlineTime = config.getOrDefault("onlineTime", "60");
        ONLINE_KEEP = Integer.parseInt(onlineTime) * 60 * 1000;

        return running = true;
    }

    @Override
    public boolean stop(VideoServer videoServer) {
        running = false;
        return running;
    }

    @Override
    public boolean alive(VideoServer videoServer) {
        if(running){
            BroadcastDiscovery.discovery();
            online();
        }
        return running;
    }

    /**
     * 判断设备是否在线
     */
    public void online(){
        for(OnvifCamera camera:DiscoveryDeviceImpl.cameraMap.values()){
            if(camera.getCameraId() == null){
                continue;
            }
            if(camera.getLastTime() + ONLINE_KEEP < System.currentTimeMillis()
                &&(camera.getCameraOnline() == null || camera.getCameraOnline().equalsIgnoreCase("N"))
            ){
                //离线转在线
                camera.setCameraOnline("Y");
                VideoCamera videoCamera = videoCameraService.selectVideoCameraById(camera.getCameraId());
                videoCamera.setOnline("Y");
                pushServer.push(PushMessage.DEVICE_ONLINE, videoCamera.getId(), videoCamera);
            }else if(camera.getLastTime() + ONLINE_KEEP > System.currentTimeMillis()
                    &&(camera.getCameraOnline() == null || camera.getCameraOnline().equalsIgnoreCase("Y"))
            ){
                //在线转离线
                camera.setCameraOnline("N");
                VideoCamera videoCamera = videoCameraService.selectVideoCameraById(camera.getCameraId());
                videoCamera.setOnline("N");
                pushServer.push(PushMessage.DEVICE_OFFLINE, videoCamera.getId(), videoCamera);
            }
        }
    }

    /**
     * 尝试登录
     * @param userName
     * @param password
     * @return
     */
    public void tryLogin(String userName, String password){
        discoveryDevice.tryLogin(userName, password);
    }
}
